/* add user code begin Header */
/**
 **************************************************************************
 * @file     main.c
 * @brief    main program
 **************************************************************************
 *                       Copyright notice & Disclaimer
 *
 * The software Board Support Package (BSP) that is made available to
 * download from Artery official website is the copyrighted work of Artery.
 * Artery authorizes customers to use, copy, and distribute the BSP
 * software and its related documentation for the purpose of design and
 * development in conjunction with Artery microcontrollers. Use of the
 * software is governed by this copyright notice and the following disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
 * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
 * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
 * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
 * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 **************************************************************************
 */
/* add user code end Header */

/* Includes ------------------------------------------------------------------*/
#include "at32f415_wk_config.h"

/* private includes ----------------------------------------------------------*/
/* add user code begin private includes */
#include "i2c_application.h"
#include "mlx90640_adapter.h"
#include "usb_data_transfer.h"
#include "usb_conf.h"
#include "usb_core.h"
#include "usbd_int.h"
#include "cdc_class.h"
#include "cdc_desc.h"
#include "multi_button.h"
/* add user code end private includes */

/* private typedef -----------------------------------------------------------*/
/* add user code begin private typedef */

/* add user code end private typedef */

/* private define ------------------------------------------------------------*/
/* add user code begin private define */
#define I2Cx_PORT I2C1
/* add user code end private define */

/* private macro -------------------------------------------------------------*/
/* add user code begin private macro */

/* add user code end private macro */

/* private variables ---------------------------------------------------------*/
/* add user code begin private variables */
i2c_handle_type hi2cx;
static float g_thermal_data[MLX90640_PIXEL_NUM];
otg_core_type otg_core_struct;

uint8_t mlx90640_erro_flag = 0;
struct Button btnok;
extern uint8_t g_receive_ok;
/* add user code end private variables */

/* private function prototypes --------------------------------------------*/
/* add user code begin function prototypes */

/* add user code end function prototypes */

/* private user code ---------------------------------------------------------*/
/* add user code begin 0 */
uint8_t read_button_GPIO(uint8_t button_id)
{
    // you can share the GPIO read function with multiple Buttons
    switch (button_id) {
        case 1:
            return gpio_input_data_bit_read(GPIOA, GPIO_PINS_15);
        default:
            return 0;
    }
}

void BTNok_LONG_PRESS_Handler(void *btn)
{
    nvic_system_reset();
}

void key_init()
{

    button_init(&btnok, read_button_GPIO, 0, 1);
    button_attach(&btnok, LONG_PRESS_START, BTNok_LONG_PRESS_Handler);
    // button_attach(&btnok, LONG_PRESS_START, BTNok_LONG_PRESS_START_Handler);
    button_start(&btnok);
}

/* add user code end 0 */

/**
 * @brief main function.
 * @param  none
 * @retval none
 */
int main(void)
{
    /* add user code begin 1 */

    nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x6000);
    key_init();
    /* add user code end 1 */

    /* system clock config. */
    wk_system_clock_config();

    /* config periph clock. */
    wk_periph_clock_config();

    /* init debug function. */
    wk_debug_config();

    /* nvic config. */
    wk_nvic_config();

    /* init usart2 function. */
    wk_usart2_init();

    /* init i2c1 function. */
    wk_i2c1_init();

    /* init tmr1 function. */
    wk_tmr1_init();

    /* init usb_otgfs1 function. */
    wk_usb_otgfs1_init();

    /* init gpio function. */
    wk_gpio_config();

    /* add user code begin 2 */

    delay_init();
    hi2cx.i2cx = I2Cx_PORT;
    mlx_param_init();

    /* add user code end 2 */

    while (1) {
        /* add user code begin 3 */
        receive_data_parse();
        mlx_set_refresh_rate();
            // printf("hello\r\n");
        if (mlx90640_erro_flag == 1)
        {
            mlx_data_read(g_thermal_data);
            usb_data_store_f(g_thermal_data);
            usb_data_send();
        }

        delay_ms(3);

        /* add user code end 3 */
    }
}
